/***************************************************************************
*  Dispatcher.hpp
*
*  July 2007
*  Copyright  2007  RuijinZheng,China
*  Email mebetop@126.com
****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
#ifndef DISPATCHER_HPP
#define DISPATCHER_HPP
#include <iostream>
using namespace std;
#include "Lowlayer.hpp"
#include "Normalflea.hpp"
#include "Detailingflea.hpp"
#include "Chainflea.hpp"
#include "Udpflea.hpp"
#include <dlfcn.h>
#include <pthread.h>

class  Dispatcher
{
public:

       Dispatcher();
       virtual ~Dispatcher();

       static bool Setsockblock(const int &sock,int &flags);
       ///set sock block
       static bool Restoresock(const int &sock,const int &flags);
       ///restore the sock to orignal stat (block or not).
       static bool Suitablesocket(int sockfd,  const sockaddr *serv_addr);
       Lowlayer * Createlayer();
       Udpflealayer * Createudplayer();

       const string &Getnetfleatype() const { return sfleatype;}

       const vector<Proxyserver> & Getproxyservers() const { return servers;}
       const old_connect &Getorigconnect() const { return oldconnect;}; 
       const old_bind &Getorigbind() const { return oldbind;}; 
       const old_recvfrom &Getorigrecvfrom() const { return oldrecvfrom;};
       const old_sendto &Getorigsendto() const { return oldsendto;};

private:

       bool loadenvironment() throw (const string &);
       bool initlibc() throw (const string &);
       void *getorignalfunc(const char * name) const throw (const string &);
       int loadservers();

private:

       void *plibc;
       old_connect oldconnect;
       old_recv oldrecv;
       old_bind oldbind;
       old_send oldsend;
       old_recvfrom oldrecvfrom;
       old_sendto oldsendto;
       vector<Proxyserver > servers;
       string sfleatype;
       string env;
       pthread_mutex_t mut;
       Normalflealayer *normal;
       Detailingflealayer *detail;
       Chainflealayer *chain;
       Udpflealayer *udp;


};
#endif /*DISPATCHER_HPP*/
